public class Solution2 {
    int[] tmp;
    public int reversePairs(int[] nums) {
        int n = nums.length;
        tmp = new int[n];
        return megerSort(nums, 0, n - 1);
    }

    // 解法一：降序
    public int megerSort(int[] nums, int left, int right) {
        if(left >= right) {
            return 0;
        }

        // 1. 根据中间元素，划分两个区间
        int mid = (left + right) / 2;
        // [left, mid] [mid + 1, right]

        // 2. 分别求出左右两个区间的翻转对
        int ret = 0;
        ret += megerSort(nums, left, mid);
        ret += megerSort(nums, mid + 1, right);

        // 3.求出一左一右的翻转对 - 降序
        int cur1 = left;
        int cur2 = mid + 1;
        while(cur1 <= mid) {
            while(cur2 <= right && nums[cur2] >= nums[cur1] / 2.0) {
                cur2++;
            }
            if(cur2 > right) {
                break;
            }
            ret += right - cur2 + 1;
            cur1++;
        }

        // 4. 合并两个有序数组
        int i = 0;
        cur1 = left;
        cur2 = mid + 1;
        while(cur1 <= mid && cur2 <= right) {
            if(nums[cur1] >= nums[cur2]) {
                tmp[i++] = nums[cur1++];
            }else {
                tmp[i++] = nums[cur2++];
            }
        }
        while(cur1 <= mid) {
            tmp[i++] = nums[cur1++];
        }
        while(cur2 <= right) {
            tmp[i++] = nums[cur2++];
        }

        // 5. 还原
        for(int j = left; j <= right; j++) {
            nums[j] = tmp[j - left];
        }
        return ret;
    }

    // 解法一：降序（另一种写法）
    public int megerSort1(int[] nums, int left, int right) {
        if(left >= right) {
            return 0;
        }

        // 1. 根据中间元素，划分两个区间
        int mid = (left + right) / 2;
        // [left, mid] [mid + 1, right]

        // 2. 分别求出左右两个区间的翻转对
        int ret = 0;
        ret += megerSort1(nums, left, mid);
        ret += megerSort1(nums, mid + 1, right);

        // 3.求出一左一右的翻转对 - 降序
        int cur1 = left;
        int cur2 = mid + 1;
        while(cur1 <= mid && cur2 <= right) {
            if(nums[cur2] >= nums[cur1] / 2.0) {
                cur2++;
            }else {
                ret += right - cur2 + 1;
                cur1++;
            }
        }

        // 4. 合并两个有序数组
        int i = 0;
        cur1 = left;
        cur2 = mid + 1;
        while(cur1 <= mid && cur2 <= right) {
            if(nums[cur1] >= nums[cur2]) {
                tmp[i++] = nums[cur1++];
            }else {
                tmp[i++] = nums[cur2++];
            }
        }
        while(cur1 <= mid) {
            tmp[i++] = nums[cur1++];
        }
        while(cur2 <= right) {
            tmp[i++] = nums[cur2++];
        }

        // 5. 还原
        for(int j = left; j <= right; j++) {
            nums[j] = tmp[j - left];
        }
        return ret;
    }

    // 解法二：升序
    public int megerSort3(int[] nums, int left, int right) {
        if(left >= right) {
            return 0;
        }

        // 1. 根据中间元素，划分两个区间
        int mid = (left + right) / 2;
        // [left, mid] [mid + 1, right]

        // 2. 分别求出左右两个区间的翻转对
        int ret = 0;
        ret += megerSort3(nums, left, mid);
        ret += megerSort3(nums, mid + 1, right);

        // 3.求出一左一右的翻转对 - 降序
        int cur1 = left;
        int cur2 = mid + 1;
        while(cur2 <= right) {
            while(cur1 <= mid && nums[cur2] >= nums[cur1] / 2.0) {
                cur1++;
            }
            if(cur1 > mid){
                break;
            }
            ret += mid - cur1 + 1;
            cur2++;
        }

        // 4. 合并两个有序数组
        int i = 0;
        cur1 = left;
        cur2 = mid + 1;
        while(cur1 <= mid && cur2 <= right) {
            if(nums[cur1] <= nums[cur2]) {
                tmp[i++] = nums[cur1++];
            }else {
                tmp[i++] = nums[cur2++];
            }
        }
        while(cur1 <= mid) {
            tmp[i++] = nums[cur1++];
        }
        while(cur2 <= right) {
            tmp[i++] = nums[cur2++];
        }

        // 5. 还原
        for(int j = left; j <= right; j++) {
            nums[j] = tmp[j - left];
        }
        return ret;
    }
}
